Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source generator samples #511

Merged
merged 12 commits into from
Apr 28, 2020
Merged

Source generator samples #511

merged 12 commits into from
Apr 28, 2020

Conversation

chsienki
Copy link
Member

Adds a sample generator project which contains three example generators:

  • HelloWorldGenerator.cs : a very simple example of injecting code into a users compilation
  • XmlSettingsGenerator.cs: an example of using additional files to create classes
  • AutoNotifyGenerator.cs: a more involved sample that implements automatic INotifyPropertyChanged support, and shows how to use the SyntaxReceiver functionality.

These are still a work in progress, and we expect them to change as we evolve the feature. In the long run these are expected to represent both simple how-to's and best practices with generators (we'll probably update the SG cookbook in the roslyn repo to start pointing here for example).

The project doesn't have tests yet. I suspect we'll want to build out some tooling like we provide for analyzers for testing generators.

@chsienki chsienki requested a review from a team as a code owner April 15, 2020 03:14
@jmarolf
Copy link
Contributor

jmarolf commented Apr 15, 2020

@chsienki I suggest we keep this in its own branch until the feature is complete. Then we merge this into master once its done. We can also work on the testing portion of this in that branch

@chsienki
Copy link
Member Author

@jmarolf Happy either way, I was originally going to leave it in a branch, but @jaredpar preferred it be in master, so we can easily point people to them (in @cartermp's blog post for instance)

@jmarolf
Copy link
Contributor

jmarolf commented Apr 15, 2020

@chsienki this link appears to work today. Would like to understand why that is not sufficient for @cartermp

@jaredpar
Copy link
Member

I would prefer they be in master. This makes them more discoverable and hopefully in turns encourages customers to interact with them.

This is a samples section and these are samples of our work in progress. The compiler changes are in master even though they're not complete. Feels odd if the supporting samples weren't. If we want to make it clearer that they're not RTM we can make a nice README to discuss that.

@jmarolf
Copy link
Contributor

jmarolf commented Apr 16, 2020

I would be fine with a readme explaining that these are a work-in-progress. I am just concerned that we would have a bunch of shipped samples next to some in-progress ones with no differentiation

@chsienki
Copy link
Member Author

Added a readme to explain they're in progress. Happy to play with the wording to get something we all like.

@cartermp
Copy link
Contributor

How do I build these samples? install.ps1 just errors out:

image


<ItemGroup>
<!--<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8" />-->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0-3.20207.2" PrivateAssets="all" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently emits a NuGet warning about depending on Microsoft.CodeAnalysis.Analyzers, picking 3.0.0-beta2.final.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@chsienki
Copy link
Member Author

The install.ps1 are for installing the analyzers as a package, but we don't support it for generators yet. Removed to clear up confusion.

// begin creating the source we'll inject into the users compilation
StringBuilder sourceBuilder = new StringBuilder(@"
using System;
namespace HelloWorldGenerator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to have this be HelloWorldGenerated to distinguish from the name of the generator

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@cartermp
Copy link
Contributor

In the README there should be instructions on how to install the sample generators into your own project. Since there's no example here showing consumption, folks will definitely want the prescriptive guidance.

- Add demo consumption project
- Update readme
- Lock samples to correct .net version via global.json
@chsienki
Copy link
Member Author

chsienki commented Apr 25, 2020

@cartermp I Added a demo project that consumes the samples, and allows a 'one click' run experience to see what they do, and how to consume them.

I updated the readme to include how to build and consume in your own project.

I added a solution in the SourceGenerators dir so you can just open the generators sample + demo project without anything else.


Building the samples
-----
Open `SourceGenerators.sln` in Visual Studio or run `dotnet build` from the `\SourceGenerators` directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see SourceGenerators.sln in this PR or repo. Did you mean to open Samples.sln?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it might help if I added that file to the repo huh?

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGenerators", "SourceGenerators", "{14D18F51-6B59-49D5-9AB7-08B38417A459}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorSamples", "samples\CSharp\SourceGenerators\SourceGeneratorSamples\SourceGeneratorSamples.csproj", "{2ADE5CFA-5DF4-44A9-BD67-E884BCFBA045}"
EndProject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be missing the consumption project.

Copy link
Contributor

@cartermp cartermp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one looks good enough to use and link to from the blog post. @jaredpar @jmarolf can you take another look?

@jaredpar
Copy link
Member

Why did you need to downgrade the demo? This is now using a non-supported netcoreapp3.0 version. Is this a problem with the SDK repo? If so we can tackle in a later PR.

@chsienki
Copy link
Member Author

Yeah, it wasn't able to build netcoreapp3.1

Looks like that just pushed the problem further though, because it's now using a compiler that doesn't support SourceGenerators.

I think I'll just remove them from the main 'shipping' samples.sln and keep the 'special' SourceGenerators.sln for now. We won't get CI, but we can fix that up in another PR?

@jmarolf
Copy link
Contributor

jmarolf commented Apr 28, 2020

@chsienki what was the original failure about? Do we need to install a runtime for .NET 3.1 or update the sdk in the global.json file?

@chsienki
Copy link
Member Author

chsienki commented Apr 28, 2020

@jmarolf https://dev.azure.com/dnceng/public/_build/results?buildId=619533&view=logs&j=a846d25a-e32c-5640-1b53-e815fab94407&t=aafa82ad-f831-5b30-c619-9b12d27e8ee8

I think we'll need to update the global.json. I added one for the SourceGenerators.sln but its using a beta version of the sdk right now.

Wasn't sure if we'd want the shipping samples in the repo depening on a pre-release SDK?

@jmarolf
Copy link
Contributor

jmarolf commented Apr 28, 2020

ah change the global.json from this

{
  "tools": {
    "dotnet": "3.0.101",
    "vs": {
      "version": "16.3"
    },
    "xcopy-msbuild": "16.3.0-alpha"
  },
  "msbuild-sdks": {
    "Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20117.3"
  }
}

to this

{
  "tools": {
    "dotnet": "3.1.101",
    "vs": {
      "version": "16.3"
    },
    "xcopy-msbuild": "16.3.0-alpha"
  },
  "msbuild-sdks": {
    "Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20117.3"
  }
}

@chsienki
Copy link
Member Author

That won't fix it though, because 3.1.101 doesn't include the source generator support.

So we need to either make the whole repo depend on 3.1.300 or remove them from the main samples solution.

@jmarolf
Copy link
Contributor

jmarolf commented Apr 28, 2020

ah, ok. I am fine with that, There are lots of repos that depend on un-released versions of the SDK

Remove special source generators one
fix reference in release
@chsienki chsienki merged commit a560eb2 into master Apr 28, 2020
@jaredpar
Copy link
Member

🎉

@sharwell sharwell deleted the source-generator-samples branch May 1, 2020 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants